package com.bf.hotpoint.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.alibaba.fastjson.JSON;
import com.bf.hotpoint.common.Singleton;
import com.bf.hotpoint.domain.TestObject;
import com.bf.hotpoint.domain.User;
import com.bf.hotpoint.service.TestService;
import com.bf.hotpoint.service.impl.TestInitMongoService;

@Controller
@RequestMapping("/test")
public class TestCotroller {

	
	@Autowired
	private TestService testService;

	@Autowired
	private TestInitMongoService ims;
	
	@RequestMapping(value="/useothertemp.html",method= {RequestMethod.POST,RequestMethod.GET})
	public ModelAndView showDate(){
		Date date = new Date();
		return new ModelAndView("date","date",date);
	}
	
	@RequestMapping(value="/toform.html",method= {RequestMethod.GET})
	public ModelAndView toForm(String today){
		return new ModelAndView("toForm","date",today);
	}
	
	@RequestMapping(value="/submitform.html",method= {RequestMethod.POST})
	public ModelAndView testTemp(String name,String age){
		System.out.println(name);
		System.out.println(age);
		return new ModelAndView("/formResult","name",name).addObject("age", age);
	} 
	
	@RequestMapping(value="/redirect.html",method= {RequestMethod.GET})
	public ModelAndView Redirect(){
		return new ModelAndView(new RedirectView("toRedirect.html"),"name","罗若峰");
	} 
	
	@RequestMapping(value="/toRedirect.html",method= {RequestMethod.GET})
	public ModelAndView toTarget(String name){
		try {
			return new ModelAndView("toRedirect","name",new String(name.getBytes("ISO-8859-1"),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping(value="/toform2.html",method= {RequestMethod.GET})
	public ModelAndView toForm(){
		return new ModelAndView("toForm2");
	}
	
	@RequestMapping(value="/submitform2.html",method= {RequestMethod.POST})
	public ModelAndView testTemp(User user){
		return new ModelAndView("formResult2","user",user);
	}
	
	
	//test json as result
	@RequestMapping(value = "/setJson", method = RequestMethod.GET)
	public @ResponseBody TestObject setJson(){
		TestObject testObj = new TestObject();
		testObj.setAge(11);
		testObj.setName("aa");
		
		List<String> attrs = new ArrayList<String>();
		attrs.add("a");
		attrs.add("b");
		testObj.setAddrs(attrs);
		return testObj;
	}
	
	
	//test json as result
	//http://localhost:8080/test/getJson?jsonstring={"addrs":["a","b"],"age":11,"name":"aa"}
	@RequestMapping(value = "/getJson", method = RequestMethod.GET)
	public @ResponseBody TestObject getJson(String jsonstring){
		TestObject to = JSON.parseObject(jsonstring,TestObject.class);

		String jsonString = JSON.toJSONString(to);
		
		System.out.println(to.getName());
		System.out.println(to.getAge());
	
		return to;
	}
	
	//test mvc
	@RequestMapping(value = "/mvc", method = RequestMethod.GET)
	public ModelAndView mvc(){
		return null;
	}	
	
	//test mongo delete
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public ModelAndView removeFirstObj(){
		ims.deleteOjb();
		return null;
	}	
	
	//test mongo location
	@RequestMapping(value = "/near/{dis}", method = RequestMethod.GET)
	public ModelAndView near(@PathVariable int dis){
		ims.near(dis/111);
		return null;
	}
	
	//test mongo location
	@RequestMapping(value = "/in/{dis}", method = RequestMethod.GET)
	public ModelAndView inCircle(@PathVariable double dis){
		ims.in(dis/111.12);
		return null;
	}
	
	//test mongo update
		@RequestMapping(value = "/update/{old}/{newname}", method = RequestMethod.GET)
		public ModelAndView updateName(@PathVariable String old,@PathVariable String newname){
			ims.updateName(old, newname);
			return null;
		}
	
	//test redis hash
	@RequestMapping(value = "/hash/{hashkey}/{age}/{name}", method = RequestMethod.GET)
	public ModelAndView hash1(@PathVariable String hashkey,@PathVariable int age,@PathVariable String name){
		testService.addToHash(hashkey, age, name);
		return null;
	}
	
	//test redis hash
	@RequestMapping(value = "/set/{age}/{name}", method = RequestMethod.GET)
	public ModelAndView set(@PathVariable int age,@PathVariable String name){
		testService.addToSet(age, name);
		return null;
	}
	
	//test redis hash
	@RequestMapping(value = "/list/{name}", method = RequestMethod.GET)
	public ModelAndView list(@PathVariable String name){
		testService.addLink(name);
		return null;
	}
	
	//test redis call back
	@RequestMapping(value = "/usecallback", method = RequestMethod.GET)
	public ModelAndView list(){
		testService.useCallback();
		return null;
	}
	
	//test redis call back
	@RequestMapping(value = "/single", method = RequestMethod.GET)
	public ModelAndView single(){
		Singleton singleton = Singleton.INSTANCE;
		singleton.printFavorites();
		return null;
	}
}
